//
//  DDAddShareAccountViewController.m
//  DingDongZP
//
//  Created by 杨政涛 on 16/10/17.
//  Copyright © 2016年 51ledian.com. All rights reserved.
//

#import "DDAddShareAccountViewController.h"
#import "DDMoneybagHomepageService.h"


@interface DDAddShareAccountViewController ()<UITextFieldDelegate>

@property(nonatomic ,strong)NSString *remarkString;

@property(nonatomic ,strong)NSString *phoneString;

@property(nonatomic,strong)DDMoneybagHomepageService *moneybagHomepageService;

@property(nonatomic ,strong)NSString *saveResult;

@end

@implementation DDAddShareAccountViewController

- (void)viewWillAppear:(BOOL)animated
{
    [self.rdv_tabBarController setTabBarHidden:true animated:true];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor colorWithWhite:0.953 alpha:1.000];
    
    self.moneybagHomepageService = [[DDMoneybagHomepageService alloc]init];
    
    [self configureUI];
}

- (void)configureUI
{
 
    NSArray *titleArray = @[@"   备注",@"   手机号码"];
    
    for (int i = 0; i<titleArray.count; i++) {
        
        UITextField *field = [[UITextField alloc]initWithFrame:CGRectMake(0, TableViewSectionHeight+61*i, SCREEN_WIDTH, 60)];
        field.tag = 1+i;
        field.backgroundColor = [UIColor whiteColor];
        field.textColor = COLOR_FONT_DARK_GRAY;
        field.font = [UIFont systemFontOfSize:14];
        field.returnKeyType = UIReturnKeyDone;
        field.clearButtonMode = UITextFieldViewModeWhileEditing;
        
        if (self.adminAccountMessage.count == 2 ) {
            
            field.text = self.adminAccountMessage[i];
        }
        
        field.delegate = self;
        [self.view addSubview:field];
        
        if (i == 0 ) {
            
            UIView *divideLine = [[UIView alloc]initWithFrame:CGRectMake(0, CGRectGetMaxY(field.frame)+0.5, SCREEN_WIDTH, 0.5)];
            divideLine.backgroundColor = [UIColor colorWithWhite:0.863 alpha:1.000];
            [self.view addSubview:divideLine];
        }
        
        if (i == 1) {
            
            field.keyboardType = UIKeyboardTypeNumberPad;
            
            UIButton *saveButton = [UIButton buttonWithType:UIButtonTypeCustom];
            saveButton.backgroundColor = COLOR_FONT_MAIN_GREEN;
            saveButton.layer.cornerRadius = 5;
            [saveButton setTitle:@"保存" forState:UIControlStateNormal];
            [saveButton addTarget:self action:@selector(saveAction) forControlEvents:UIControlEventTouchUpInside];
            [self.view addSubview:saveButton];
            
            WEAKSELF
            [saveButton mas_makeConstraints:^(MASConstraintMaker *make) {
               
                make.top.mas_equalTo(field.mas_bottom).offset(60);
                make.left.equalTo(weakSelf.view).offset(30);
                make.right.equalTo(weakSelf.view).offset(-30);
                make.height.equalTo(@(50));
            }];
        }
        
        UILabel *lab = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 90, CGRectGetHeight(field.frame))];
        lab.text = titleArray[i];
        lab.font = [UIFont systemFontOfSize:16];
        lab.textColor = COLOR_FONT_BLACK;
        field.leftView = lab;
        field.leftViewMode = UITextFieldViewModeAlways;
    }
}

- (void)saveAction
{
    for (int i = 0; i<2; i++) {
        
        UITextField *field = (UITextField *)[self.view viewWithTag:1+i];
        [field resignFirstResponder];
        
        if (i == 0) {
            
            self.remarkString = field.text;
        }
        
        if (i == 1) {
            
            self.phoneString = field.text;
        }
    }
    
    WEAKSELF
    if (self.remarkString.length == 0) {
        
        [weakSelf showLDAlertView:@"请先填写备注"];
    }else{
    
        if (self.phoneString.length == 0) {
            
            [weakSelf showLDAlertView:@"请填写手机号码"];
        }else{
            
            NSString *phoneRegex = @"^(0|86|17951)?(13[0-9]|15[012356789]|17[678]|18[0-9]|14[57])[0-9]{8}$";
            NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", phoneRegex];//正则
            BOOL isMatch = [pred evaluateWithObject:self.phoneString];
            if (isMatch) {
                
                if (self.currentID.length == 0) {
                    
                    self.currentID = @"";
                }
                [weakSelf saveDataWithid:self.currentID withRemark:self.remarkString withShare_mobile:self.phoneString];
            }else
            {
                [weakSelf showLDAlertView:@"请输入正确的手机号"];
            }
        }
    }
}


- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    for (int i = 0; i<2; i++) {
        
        UITextField *field = (UITextField *)[self.view viewWithTag:1+i];
        [field resignFirstResponder];
    }

}

#define UITextFieldDelegate

- (void)textFieldDidEndEditing:(UITextField *)textField
{
    for (int i = 0; i<2; i++) {
        
        UITextField *field = (UITextField *)[self.view viewWithTag:1+i];
        
        if (i == 0) {
            
            self.remarkString = field.text;
        }
      
        if (i == 1) {
            
            self.phoneString = field.text;
        }
    }
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [textField resignFirstResponder];
    
    return YES;
}

// 网络请求 保存数据
- (void)saveDataWithid:(NSString *)currentID  withRemark:(NSString *)remark withShare_mobile:(NSString *)share_mobile
{
    
    WEAKSELF
    [self.moneybagHomepageService saveShareAccountWithid:currentID withRemark:remark withShare_mobile:share_mobile SuccessBlock:^(id responseObject) {
        
        self.saveResult = [NSString stringWithFormat:@"%@",responseObject[@"msg"]];
        
        [self performSelectorOnMainThread:@selector(backToMainToUpdata) withObject:self waitUntilDone:YES];
        
    } failBlock:^(id responseObject, NSError *error) {
        
        [weakSelf requestFaild];
    }];
}

- (void)backToMainToUpdata
{
    
    WEAKSELF
    if ([self.saveResult isEqualToString:@"成功！"]) {
        
        [weakSelf.navigationController popViewControllerAnimated:YES];
    }else{
    
        [weakSelf showLDAlertView:self.saveResult];
    }
}

- (void)viewWillDisappear:(BOOL)animated
{
    
    [self.rdv_tabBarController setTabBarHidden:false animated:true];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
